<template>
  <div class="todo-list">
    <input v-model="newTodoTitle" placeholder="添加新的待办事项" />
    <button @click="addTodo">添加</button>
    <ul>
      <li v-for="(todo, index) in todos" :key="index">
        <TodoItem
          :title="todo.title"
          :completed="todo.completed"
          @update:completed="updateTodoCompleted"
          @remove-todo="removeTodo"
        />
      </li>
    </ul>
  </div>
</template>

<script>
import TodoItem from "./TodoItem.vue";

export default {
  components: {
    TodoItem,
  },
  data() {
    return {
      newTodoTitle: "",
      todos: [
        { title: "复习计算机知识", completed: false },
        { title: "游泳60分钟", completed: false },
        { title: "跑步20分钟", completed: false },
      ],
    };
  },
  methods: {
    addTodo() {
      if (this.newTodoTitle) {
        this.todos.push({ title: this.newTodoTitle, completed: false });
        this.newTodoTitle = "";
      }
    },
    removeTodo(index) {
      this.todos.splice(index, 1);
    },
    updateTodoCompleted(index, completed) {
      this.todos[index].completed = completed;
    },
  },
};
</script>

<style scoped>
.todo-list {
  display: flex;
  flex-direction: column;
}
</style>